package com.itcast.yygh.cmn.controller;

import com.itcast.common.result.R;
import com.itcast.yygh.cmn.service.DictService;
import com.itcast.yygh.model.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典服务模块
 */
@Api(tags = "数据字典服务模块")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    //引入service的依赖
    @Resource
    private DictService dictService;

    //根据id查询数据字典子数据列表
    @ApiOperation("根据id查询数据字典子数据列表")
    @GetMapping("/findChildData/{id}")
    public R findChildData(@PathVariable Long id) {
        //调用service中的查询方法
        List<Dict> dicts = dictService.findChildData(id);
        //返回数据
        return R.ok().data("list", dicts);
    }

    //数据字典导出功能
    @ApiOperation("数据字典列表导出功能")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    //数据字典导入功能
    @ApiOperation("数据字典导入功能")
    @PostMapping("/importData")
    public R importData(MultipartFile file) {
        dictService.importData(file);
        return R.ok();
    }

    //根据数据字典父类的id和value查询数据字典的name
    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{parentDictCode}/{value}")
    public String getName(
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    //根据value查询数据字典的名称
    @ApiOperation("获取数据字典名称")
    @GetMapping("/getName/{value}")
    public String getName(
            @ApiParam(name = "value", value = "值", required = true)
            @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(null, value);
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public R findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list", list);
    }
}
